﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<%@ Register Src="../Shared/Header.ascx" TagName="Header" TagPrefix="uc1" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>菜单管理</title>
    <uc1:Header ID="Header1" runat="server" />
    <script type="text/javascript" language="javascript">
        $(function () {

            $('#grid').treegrid({
                iconCls: 'icon-ok',
                nowrap: false,                      //数据是否显示在一行里
                striped: true,                      //奇偶行使用不同背景色
                url: '/Menu/LoadAll/',              //从远程站点请求数据的 URL
                fitColumns: true,                   //适应表格的宽度并且防止水平滚动-
                fit: true,
                idField: 'id',                      //标识字段
                treeField: 'name',                  //定义树节点的字段
                sortName: 'index',                  //定义可以排序的列
                sortOrder: 'asc',                   //定义列的排序顺序，只能用 'asc' 或 'desc'
                remoteSort: false,                  //定义是否从服务器给数据排序
                singleSelect: true,                //是否允许选中一行
                pagination: false,                  //分页
                rownumbers: false,                   //行号
                animate: true,                      //定义当节点展开或折叠时是否显示动画效果。
                columns: [[
                     { field: 'name', title: '菜单名称', width: 80 }
                    , { field: 'code', title: '代码', width: 80 }
                    , { field: 'index', title: '排序', width: 40 }
                    , { field: 'url', title: '地址', width: 80 }
                    , { field: 'description', title: '描述', width: 120 }
                    , { field: 'ispage', title: '是否页面', width: 40,
                        formatter: function (value, rowData, rowIndex) {
                            return value ? "是" : "否";
                        }
                    }
                    , { field: 'lever', title: '节点深度', width: 80, hidden: true }
                ]],
                onSelect: function (rowData) {
                    if (parseInt(rowData.lever) == 0) {
                        $('#btnAdd').linkbutton({ disabled: false });
                        $('#btnUpdate').linkbutton({ disabled: true });
                        $('#btnDelete').linkbutton({ disabled: true });
                    }
                    else if (parseInt(rowData.lever) == 1) {
                        $('#btnAdd').linkbutton({ disabled: false });
                        $('#btnUpdate').linkbutton({ disabled: false });
                        $('#btnDelete').linkbutton({ disabled: false });
                    }
                    else {
                        $('#btnAdd').linkbutton({ disabled: true });
                        $('#btnUpdate').linkbutton({ disabled: false });
                        $('#btnDelete').linkbutton({ disabled: false });
                    }

                    
                }
            });

        });


        function submit() {

            $('#form').form('submit', {
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (msg) {
                    var msg1 = eval('(' + msg + ')');
                    if (msg1.IsSuccess) {
                        $.messager.alert('提示', "保存成功", 'info', function () {
                            //                            $('#win').dialog('close');
                            $('#grid').treegrid('reload');
                        });
                    }
                    else {
                        $.messager.alert('提示', msg1.Message, "error");
                    }
                },
                error: function () {
                    $.messager.alert('错误', '保存失败！', "error");
                }
            });
        }

        function add() {
            var row = $('#grid').treegrid('getSelected');
            if (row) {
                $('#win').dialog({
                    title: '新增菜单',
                    iconCls: 'icon-add',
                    width: 400,
                    height: 284,
                    closed: false,
                    cache: false,
                    href: '/Menu/Add/' + row.id,
                    modal: true,
                    buttons: [{
                        text: '提交',
                        iconCls: 'icon-ok',
                        handler: submit
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#win').dialog('close');
                        }
                    }],
                    onLoad: function () {
                        $("#ActionName").combogrid({
                            panelWidth: 520,
                            panelHeight: 308,
                            idField: 'ActionName',
                            textField: 'ActionName',
                            url: '/Menu/LoadAllPageByAssembly/',
                            singleSelect: true,                //是否允许选中一行
                            pagination: true,                  //分页
                            rownumbers: true,                   //行号
                            pageSize: 10,                        //每页记录数
                            pageList: [10],             //每页记录数列表
                            columns: [[
					                    { field: 'ActionName', title: '功能名称', width: 150 }
                                        , { field: 'ControllerName', title: '模块', width: 120 }
                                        , { field: 'Description', title: '描述', width: 200 }
				                    ]],
                            onSelect: function (rowIndex, rowData) {
                                //alert(rowIndex + "\n" + rowData);
                                $("#ControllerName").val(rowData.ControllerName);
                                $("#ControllerName1").val(rowData.ControllerName);
                                $("#Description").val(rowData.Description);
                            },
                            delay: 500,
                            mode: 'remote',
                            onChange: function (newValue, oldValue) {
                                $("#ControllerName").val("");
                                $("#Description").val("");
                            }
                        });
                    }
                });
            }
            else {
                $.messager.alert('提示', '请选择要新增的上级菜单');
                return;
            }
        }

        function edit() {
            var row = $('#grid').treegrid('getSelected');
            if (row) {
                var tempUrl = "";
                if (row.url.indexOf('/') != -1) {
                    tempUrl = row.url.substr(row.url.lastIndexOf('/') + 1);
                }
                $('#win').dialog({
                    title: '修改菜单',
                    iconCls: 'icon-save',
                    width: 400,
                    height: 284,
                    closed: false,
                    cache: false,
                    href: '/Menu/Edit/' + row.id,
                    modal: true,
                    buttons: [{
                        text: '提交',
                        iconCls: 'icon-ok',
                        handler: submit
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#win').dialog('close');
                        }
                    }],
                    onLoad: function () {
                        $("#ActionName").combogrid({
                            panelWidth: 520,
                            panelHeight: 308,
                            value: tempUrl,
                            idField: 'ActionName',
                            textField: 'ActionName',
                            url: '/Menu/LoadAllPageByAssembly/',
                            singleSelect: true,                //是否允许选中一行
                            pagination: true,                  //分页
                            rownumbers: true,                   //行号
                            pageSize: 10,                        //每页记录数
                            pageList: [10],             //每页记录数列表
                            columns: [[
					            { field: 'ActionName', title: '功能名称', width: 150 }
                                , { field: 'ControllerName', title: '模块', width: 120 }
                                , { field: 'Description', title: '描述', width: 200 }
				            ]]
                            
                        });
                    }
                });
            } else {
                $.messager.alert('提示', '请选择要修改的数据');
                return;
            }
        }

        function del() {
            var rows = $('#grid').treegrid('getSelections');
            if (!rows || rows.length == 0) {
                $.messager.alert('提示', '请选择要删除的数据');
                return;
            }
            var parm;
            $.each(rows, function (i, n) {
                if (i == 0) {
                    parm = "idList=" + n.id;
                }
                else {
                    parm += "&idList=" + n.id;
                }
            });
            $.messager.confirm('提示', '是否删除这些数据?', function (r) {
                if (!r) {
                    return;
                }

                $.ajax({
                    type: "POST",
                    url: "/Menu/Delete/",
                    data: parm,
                    success: function (msg) {
                        if (msg.IsSuccess != null) {
                            if (msg.IsSuccess) {
                                $.messager.alert('提示', '删除成功！', "info", function () {
                                    $('#grid').treegrid("reload");
                                });
                            }
                            else {
                                $.messager.alert('提示', msg.Message, "error");
                            }
                        } 
                        else {
                            eval(msg);
                        }
                    },
                    error: function () {
                        $.messager.alert('错误', '删除失败！', "error");
                    }
                });
            });
        }
    </script>
</head>
<body class="easyui-layout">
    <div region="center" style="padding: 5px;" border="false">
        <table id="grid" toolbar="#toolbar">
        </table>
        <div id="toolbar" style="padding: 2px; height: auto">
            <a id="btnAdd" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-add" plain="true" onclick="add()" disabled>添加</a>
            <a id="btnUpdate" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-edit" plain="true" onclick="edit()" disabled>修改</a>
            <a id="btnDelete" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-remove" plain="true" onclick="del()" disabled>删除</a>
        </div>
    </div>
    <div id="win" />
</body>
</html>
